//
//  LGPatientManageController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/2/13.
//

//#import "HeartDiseaseManage-Swift.h"
#import "LGPatientHomeController.h"
//患者列表
#import "LGPatientTableController.h"
//搜索
#import "LGPatientSearchController.h"
#import "LGBottomShadowView.h"
#import "LGPatientModel.h"

#define viewHeight  (Screen_H-topBarHeight-tabBarHeight-viewPix(7))

@interface LGPatientHomeController ()<UIScrollViewDelegate>
/**   */
@property (nonatomic , strong)LGPatientTableController *myPatientVC;
/**   */
@property (nonatomic , strong)LGPatientTableController *teamPatientVC;
/**   */
@property (nonatomic , strong)LGPatientTableController *consultPatientVC;
/**   */
@property (nonatomic , strong)UIImageView *bgImageView;
/**   */
@property (nonatomic , strong)LGBottomShadowView *shadowView;
/**   */
@property (nonatomic , strong)UIButton *myPatient;
/**   */
@property (nonatomic , strong)UIButton *teamPatient;
/**   */
@property (nonatomic , strong)UIButton *consultPatient;
/**   */
@property (nonatomic , strong)UIView *lineView;
/**   */
@property (nonatomic , strong)UIButton *searchBtn;
/**   */
@property (nonatomic , strong)UIView *cornerView;
/**   */
@property (nonatomic , strong)UIScrollView *scrollView;
/**   */
@property (nonatomic , strong)NSMutableArray *mineArray;
/**   */
@property (nonatomic , strong)NSMutableArray *teamArray;
/**   */
@property (nonatomic , strong)NSArray *titleArray;

@end

@implementation LGPatientHomeController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.roleType = @"";
    [self createSubView];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self viewWillAppearAction];
}


-(void)viewWillAppearAction{
    if(![self.roleType isEqualToString:kPatientval]){
        [self layoutRoleView];
    }
}

//更换角色后刷新
-(void)layoutRoleView{
    self.myPatient.titleLabel.font = LGFontWeight(18, UIFontWeightSemibold);
    self.teamPatient.titleLabel.font = LGFont(16);
    self.consultPatient.titleLabel.font = LGFont(16);
    self.lineView.frame = CGRectMake(viewPix(43), statusBarHeight+viewPix(36), viewPix(16), 3);
    //99:队长医生   10:专家医生  11:进修医生  40:康复医生  20:护士  30:个案管理师    100:其他
    if([kPatientval isEqualToString:@"99"]){
        //医生--团队长能查看个人患者、团队患者、咨询患者
        self.myPatient.hidden = NO;
        self.teamPatient.hidden = NO;
        self.consultPatient.hidden = NO;
        self.myPatient.userInteractionEnabled = YES;
        self.teamPatient.userInteractionEnabled = YES;
        self.consultPatient.userInteractionEnabled = YES;
    
        [self.myPatient setTitleColor:[UIColor colorWithString:@"#4C87FD"] forState:UIControlStateNormal];
        [self.teamPatient setTitleColor:[UIColor colorWithString:@"#666666"] forState:UIControlStateNormal];
        [self.myPatient mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).offset(statusBarHeight+viewPix(10));
            make.left.equalTo(self.view).offset(viewPix(16));
            make.height.equalTo(@(viewPix(25)));
        }];
        [self.teamPatient mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.myPatient.mas_right).offset(viewPix(16));
            make.centerY.height.equalTo(self.myPatient);
        }];
        [self.consultPatient mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.teamPatient.mas_right).offset(viewPix(16));
            make.centerY.height.equalTo(self.teamPatient);
        }];
        self.myPatientVC.view.frame = CGRectMake(0, 0, Screen_W, viewHeight);
        self.teamPatientVC.view.frame = CGRectMake(Screen_W, 0, Screen_W, viewHeight);
        self.consultPatientVC.view.frame = CGRectMake(Screen_W*2, 0, Screen_W, viewHeight);
        self.scrollView.contentSize = CGSizeMake(Screen_W*3, viewHeight);
        self.scrollView.contentOffset = CGPointMake(0, 0);
        self.scrollView.scrollEnabled = YES;
        
    }else if ([kPatientval isEqualToString:@"10"] || [kPatientval isEqualToString:@"11"]){
        //专家、进修医生--只能查看个人患者、咨询患者
        self.myPatient.hidden = NO;
        self.teamPatient.hidden = YES;
        self.consultPatient.hidden = NO;
        self.myPatient.userInteractionEnabled = YES;
        self.teamPatient.userInteractionEnabled = NO;
        self.consultPatient.userInteractionEnabled = YES;
        [self.myPatient setTitleColor:[UIColor colorWithString:@"#4C87FD"] forState:UIControlStateNormal];
        [self.myPatient mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).offset(statusBarHeight+viewPix(10));
            make.left.equalTo(self.view).offset(viewPix(16));
            make.height.equalTo(@(viewPix(25)));
        }];
        [self.consultPatient mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.myPatient.mas_right).offset(viewPix(16));
            make.centerY.height.equalTo(self.myPatient);
        }];
        self.myPatientVC.view.frame = CGRectMake(0, 0, Screen_W, viewHeight);
        self.teamPatientVC.view.frame = CGRectMake(Screen_W*2, 0, Screen_W, viewHeight);
        self.consultPatientVC.view.frame = CGRectMake(Screen_W, 0, Screen_W, viewHeight);
        self.scrollView.contentSize = CGSizeMake(Screen_W*2, viewHeight);
        self.scrollView.contentOffset = CGPointMake(0, 0);
        self.scrollView.scrollEnabled = YES;

    }else if([kPatientval isEqualToString:@"40"]){
        //康复--只能查看团队患者、咨询患者
        self.myPatient.hidden = YES;
        self.teamPatient.hidden = NO;
        self.consultPatient.hidden = NO;
        self.myPatient.userInteractionEnabled = NO;
        self.teamPatient.userInteractionEnabled = YES;
        self.consultPatient.userInteractionEnabled = YES;
        self.teamPatient.titleLabel.font = LGFontWeight(18, UIFontWeightSemibold);
        self.teamPatient.frame = CGRectMake(viewPix(8), statusBarHeight+viewPix(8), viewPix(88), viewPix(30));
        [self.teamPatient setTitleColor:[UIColor colorWithString:@"#4C87FD"] forState:UIControlStateNormal];
        [self.teamPatient mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).offset(statusBarHeight+viewPix(10));
            make.left.equalTo(self.view).offset(viewPix(16));
            make.height.equalTo(@(viewPix(25)));
        }];
        [self.consultPatient mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.teamPatient.mas_right).offset(viewPix(16));
            make.centerY.height.equalTo(self.teamPatient);
        }];
        self.myPatientVC.view.frame = CGRectMake(Screen_W*2, 0, Screen_W, viewHeight);
        self.teamPatientVC.view.frame = CGRectMake(0, 0, Screen_W, viewHeight);
        self.consultPatientVC.view.frame = CGRectMake(Screen_W, 0, Screen_W, viewHeight);
        self.scrollView.contentSize = CGSizeMake(Screen_W*2, viewHeight);
        self.scrollView.contentOffset = CGPointMake(0, 0);
        self.scrollView.scrollEnabled = YES;
    }else{
        //其他（护士、个案）--只能查看团队患者
        self.myPatient.hidden = YES;
        self.teamPatient.hidden = NO;
        self.consultPatient.hidden = YES;
        self.myPatient.userInteractionEnabled = NO;
        self.teamPatient.userInteractionEnabled = NO;
        self.consultPatient.userInteractionEnabled = NO;
        self.teamPatient.titleLabel.font = LGFontWeight(18, UIFontWeightSemibold);
        self.teamPatient.frame = CGRectMake(viewPix(8), statusBarHeight+viewPix(8), viewPix(88), viewPix(30));
        [self.teamPatient setTitleColor:[UIColor colorWithString:@"#4C87FD"] forState:UIControlStateNormal];
        [self.teamPatient mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).offset(statusBarHeight+viewPix(10));
            make.left.equalTo(self.view).offset(viewPix(16));
            make.height.equalTo(@(viewPix(25)));
        }];
        [self.consultPatient mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.teamPatient.mas_right).offset(viewPix(16));
            make.centerY.height.equalTo(self.teamPatient);
        }];
        self.myPatientVC.view.frame = CGRectMake(Screen_W*2, 0, Screen_W, viewHeight);
        self.teamPatientVC.view.frame = CGRectMake(0, 0, Screen_W, viewHeight);
        self.consultPatientVC.view.frame = CGRectMake(Screen_W, 0, Screen_W, viewHeight);
        self.scrollView.contentSize = CGSizeMake(Screen_W, viewHeight);
        self.scrollView.contentOffset = CGPointMake(0, 0);
        self.scrollView.scrollEnabled = NO;
    }
    self.roleType = kPatientval;
}

//我的患者、团队患者
-(void)patientBtnAction:(UIButton *)sender{
    if(sender == self.myPatient){
        //我的患者
        [UIView animateWithDuration:0.3 animations:^{
            [self.myPatient setTitleColor:[UIColor colorWithString:@"#4C87FD"] forState:UIControlStateNormal];
            [self.teamPatient setTitleColor:[UIColor colorWithString:@"#666666"] forState:UIControlStateNormal];
            [self.consultPatient setTitleColor:[UIColor colorWithString:@"#666666"] forState:UIControlStateNormal];
            self.myPatient.titleLabel.font = LGFontWeight(18, UIFontWeightSemibold);
            self.teamPatient.titleLabel.font = LGFont(16);
            self.consultPatient.titleLabel.font = LGFont(16);
            self.lineView.center = CGPointMake(self.myPatient.center.x, self.myPatient.frame.origin.y+viewPix(25));
        }];
        for (LGPatientTableController *controller in self.mineArray) {
            [controller resetOpenSection];
        }
        CGFloat frame_X = self.myPatientVC.view.frame.origin.x;
        [self.scrollView setContentOffset:CGPointMake(frame_X, 0)];
        
    }else if(sender == self.teamPatient){
        //团队患者
        [UIView animateWithDuration:0.3 animations:^{
            [self.myPatient setTitleColor:[UIColor colorWithString:@"#666666"] forState:UIControlStateNormal];
            [self.teamPatient setTitleColor:[UIColor colorWithString:@"#4C87FD"] forState:UIControlStateNormal];
            [self.consultPatient setTitleColor:[UIColor colorWithString:@"#666666"] forState:UIControlStateNormal];
            self.teamPatient.titleLabel.font = LGFontWeight(18, UIFontWeightSemibold);
            self.myPatient.titleLabel.font = LGFont(16);
            self.consultPatient.titleLabel.font = LGFont(16);
            self.lineView.center = CGPointMake(self.teamPatient.center.x, self.teamPatient.frame.origin.y+viewPix(25));
        }];
        for (LGPatientTableController *controller in self.teamArray) {
            [controller resetOpenSection];
        }
        CGFloat frame_X = self.teamPatientVC.view.frame.origin.x;
        [self.scrollView setContentOffset:CGPointMake(frame_X, 0)];
        
    }else{
        //咨询患者
        [UIView animateWithDuration:0.3 animations:^{
            [self.myPatient setTitleColor:[UIColor colorWithString:@"#666666"] forState:UIControlStateNormal];
            [self.teamPatient setTitleColor:[UIColor colorWithString:@"#666666"] forState:UIControlStateNormal];
            [self.consultPatient setTitleColor:[UIColor colorWithString:@"#4C87FD"] forState:UIControlStateNormal];
            self.consultPatient.titleLabel.font = LGFontWeight(18, UIFontWeightSemibold);
            self.myPatient.titleLabel.font = LGFont(16);
            self.teamPatient.titleLabel.font = LGFont(16);
            self.lineView.center = CGPointMake(self.consultPatient.center.x, self.consultPatient.frame.origin.y+viewPix(25));
        }];
        for (LGPatientTableController *controller in self.teamArray) {
            [controller resetOpenSection];
        }
        CGFloat frame_X = self.consultPatientVC.view.frame.origin.x;
        [self.scrollView setContentOffset:CGPointMake(frame_X, 0)];
    }
}

//搜索
-(void)searchBtnAction{
    LGPatientSearchController *controller = [[LGPatientSearchController alloc]init];
    [self.navigationController pushViewController:controller animated:YES];
}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat percent = (CGFloat)scrollView.contentOffset.x/Screen_W;
//    percent = [kPatientval isEqualToString:@"99"]?percent:0;
    self.lineView.frame = CGRectMake((viewPix(124)-viewPix(43))*percent+viewPix(43), kMaxY(self.myPatient.frame)-1.5, viewPix(16), 3);
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    CGFloat offset = scrollView.contentOffset.x;
    if([kPatientval isEqualToString:@"99"]){
        //医生--团队长能查看个人患者、团队患者、咨询患者
        if(offset < Screen_W){
            [self patientBtnAction:self.myPatient];
            
        }else if(offset >= Screen_W && offset < Screen_W*2){
            [self patientBtnAction:self.teamPatient];
            
        }else{
            [self patientBtnAction:self.consultPatient];
        }
    }else if ([kPatientval isEqualToString:@"10"] || [kPatientval isEqualToString:@"11"]){
        //专家、进修医生--只能查看个人患者、咨询患者
        if(offset >= Screen_W){
            [self patientBtnAction:self.consultPatient];
        }else{
            [self patientBtnAction:self.myPatient];
        }

    }else if([kPatientval isEqualToString:@"40"]){
        //康复--只能查看团队患者、咨询患者
        if(offset >= Screen_W){
            [self patientBtnAction:self.consultPatient];
        }else{
            [self patientBtnAction:self.teamPatient];
        }
    }else{
        //其他（护士、个案）--只能查看团队患者
        [self patientBtnAction:self.teamPatient];
    }
}


#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    self.fd_prefersNavigationBarHidden = YES;
    self.gradientLayer.hidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    [self addChildViewController:self.myPatientVC];
    [self addChildViewController:self.teamPatientVC];
    [self addChildViewController:self.consultPatientVC];
    
    [self.view addSubview:self.bgImageView];
    [self.view addSubview:self.lineView];
    [self.view addSubview:self.searchBtn];
    [self.view addSubview:self.myPatient];
    [self.view addSubview:self.teamPatient];
    [self.view addSubview:self.consultPatient];
    [self.view addSubview:self.cornerView];
    [self.view addSubview:self.shadowView];
    [self.cornerView addSubview:self.scrollView];
    [self.scrollView addSubview:self.myPatientVC.view];
    [self.scrollView addSubview:self.teamPatientVC.view];
    [self.scrollView addSubview:self.consultPatientVC.view];
    
    [self.myPatient mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(statusBarHeight+viewPix(10));
        make.left.equalTo(self.view).offset(viewPix(16));
        make.height.equalTo(@(viewPix(25)));
    }];
    [self.teamPatient mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.myPatient.mas_right).offset(viewPix(16));
        make.centerY.height.equalTo(self.myPatient);
    }];
    [self.consultPatient mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.teamPatient.mas_right).offset(viewPix(16));
        make.centerY.height.equalTo(self.myPatient);
    }];
    [self.searchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(-viewPix(15));
        make.top.equalTo(self.view).offset(statusBarHeight+7);
        make.width.height.equalTo(@(viewPix(30)));
    }];
}

-(UIImageView *)bgImageView{
    if(!_bgImageView){
        _bgImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"患者管理-%@",AppTitle]]];
        _bgImageView.frame = CGRectMake(0, 0, Screen_W, viewPix(355));
    }
    return _bgImageView;
}

-(UIButton *)myPatient{
    if(!_myPatient){
        _myPatient = [UIButton buttonWithTitle:@"我的患者" titleFont:LGFontWeight(18, UIFontWeightSemibold) textColor:@"#4C87FD" imageName:@""];
        _myPatient.frame = CGRectMake(viewPix(8), statusBarHeight+viewPix(8), viewPix(88), viewPix(30));
        [_myPatient addTarget:self action:@selector(patientBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _myPatient;
}

-(UIButton *)teamPatient{
    if(!_teamPatient){
        _teamPatient = [UIButton buttonWithTitle:@"团队患者" titleFont:LGFont(16) textColor:@"#666666" imageName:@""];
        _teamPatient.frame = CGRectMake(kMaxX(self.myPatient.frame), statusBarHeight+viewPix(8), viewPix(88), viewPix(30));
        [_teamPatient addTarget:self action:@selector(patientBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _teamPatient;
}

-(UIButton *)consultPatient{
    if(!_consultPatient){
        _consultPatient = [UIButton buttonWithTitle:@"咨询患者" titleFont:LGFont(16) textColor:@"#666666" imageName:@""];
        _consultPatient.frame = CGRectMake(kMaxX(self.teamPatient.frame), statusBarHeight+viewPix(8), viewPix(88), viewPix(30));
        [_consultPatient addTarget:self action:@selector(patientBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _consultPatient;
}

-(UIView *)lineView{
    if(!_lineView){
        _lineView = [[UIView alloc] initWithFrame:CGRectMake(viewPix(43), statusBarHeight+viewPix(36), viewPix(16), 3)];
        _lineView.backgroundColor = [UIColor colorWithString:@"#4C87FD"];
        _lineView.cornerRidus = 2;
    }
    return _lineView;
}

-(UIButton *)searchBtn{
    if(!_searchBtn){
        _searchBtn = [UIButton buttonWithImage:@"患者管理-搜索" selectImage:@"患者管理-搜索"];
        [_searchBtn addTarget:self action:@selector(searchBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _searchBtn;
}

-(UIView *)cornerView{
    if(!_cornerView){
        _cornerView = [[UIView alloc] initWithFrame:CGRectMake(0, topBarHeight+viewPix(7), Screen_W, viewHeight+15)];
        _cornerView.backgroundColor = [UIColor clearColor];
    }
    return _cornerView;
}

-(UIScrollView *)scrollView{
    if(!_scrollView){
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, viewHeight)];
        _scrollView.contentSize = CGSizeMake(Screen_W*3, viewHeight);
        _scrollView.backgroundColor = [UIColor clearColor];
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;
    }
    return _scrollView;
}

-(LGPatientTableController *)myPatientVC{
    if(!_myPatientVC){
        _myPatientVC = [[LGPatientTableController alloc]init];
        _myPatientVC.filterAction = @"/doctor/patient.patient/filter";
        _myPatientVC.searchAction = @"/doctor/patient.patient/search";
        _myPatientVC.is_team = @"0";
        _myPatientVC.view.frame = CGRectMake(0, 0, Screen_W, viewHeight);
    }
    return _myPatientVC;
}

-(LGPatientTableController *)teamPatientVC{
    if(!_teamPatientVC){
        _teamPatientVC = [[LGPatientTableController alloc]init];
        _teamPatientVC.filterAction = @"/doctor/patient.patient/filter";
        _teamPatientVC.searchAction = @"/doctor/patient.patient/search";
        _teamPatientVC.is_team = @"1";
        _teamPatientVC.view.frame = CGRectMake(Screen_W, 0, Screen_W, viewHeight);
    }
    return _teamPatientVC;
}

-(LGPatientTableController *)consultPatientVC{
    if(!_consultPatientVC){
        _consultPatientVC = [[LGPatientTableController alloc]init];
        _consultPatientVC.filterAction = @"/doctor/patient.patient/appointmentFilter";
        _consultPatientVC.searchAction = @"/doctor/patient.patient/appointmentSearch";
        _consultPatientVC.is_team = @"";
        _consultPatientVC.view.frame = CGRectMake(Screen_W*2, 0, Screen_W, viewHeight);
    }
    return _consultPatientVC;
}

-(LGBottomShadowView *)shadowView{
    if (!_shadowView) {
        _shadowView = [[LGBottomShadowView alloc]initWithFrame:CGRectMake(0, Screen_H-tabBarHeight-20, Screen_W, 20)];
    }
    return _shadowView ;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
